package ace.module.upms.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.manager.spring.AbstractSpringDtoManager;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.module.common.core.impl.properties.ModuleCommonBaseProperties;
import ace.module.common.core.impl.properties.ModuleCommonSecurityProperties;
import ace.module.upms.api.enums.UpmsApplicationStatusEnum;
import ace.module.upms.api.model.dto.UpmsApplicationDto;
import ace.module.upms.core.impl.dao.entity.UpmsApplication;
import ace.module.upms.core.impl.dao.repository.UpmsApplicationRepository;
import ace.module.upms.core.impl.manager.UpmsApplicationManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 权限应用 manager implement.
 *
 * @author Caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class UpmsApplicationManagerImpl
    extends AbstractSpringDtoManager<UpmsApplication, Long, UpmsApplicationDto>
    implements UpmsApplicationManager {

  private final UpmsApplicationRepository upmsApplicationRepository;
  private final MessageSourceAssertService assertService;
  private final ModuleCommonBaseProperties moduleCommonBaseProperties;
  private final ModuleCommonSecurityProperties moduleCommonSecurityProperties;

  @Override
  public UpmsApplication getByOutTypeAndOutId(String outType, String outId) {
    return this.upmsApplicationRepository.getByOutTypeAndOutIdAndDeleteFlag(
        outType, outId, CoreConstant.DELETE_FLAG_NOT);
  }

  @Override
  public UpmsApplication getAndCheckByOutTypeAndOutId(String outType, String outId) {
    UpmsApplication upmsApplication = this.getByOutTypeAndOutId(outType, outId);

    this.check(upmsApplication);

    return upmsApplication;
  }

  @Override
  public UpmsApplicationDto getDtoByOutTypeAndOutId(String outType, String outId) {
    UpmsApplication upmsApplication = this.getByOutTypeAndOutId(outType, outId);
    return this.getEntityToDtoConverter().convert(upmsApplication);
  }

  @Override
  public UpmsApplicationDto getAndCheckDtoByOutTypeAndOutId(String outType, String outId) {
    UpmsApplication upmsApplication = this.getAndCheckByOutTypeAndOutId(outType, outId);
    return this.getEntityToDtoConverter().convert(upmsApplication);
  }

  @Override
  public UpmsApplication getByAdminApplication() {
    UpmsApplication upmsApplication = this.upmsApplicationRepository.getById(moduleCommonSecurityProperties.getUpmsAdminPlatformAppId());
    return upmsApplication;
  }

  @Override
  public UpmsApplicationDto getDtoByAdminApplication() {
    UpmsApplication upmsApplication = this.getByAdminApplication();
    return this.convertToEntityDto(upmsApplication);
  }

  @Override
  public UpmsApplicationDto getDtoAndCheckNotNullByAdminApplication() {
    UpmsApplication upmsApplication = this.getByAdminApplication();
    this.checkNotNul(upmsApplication);
    return this.convertToEntityDto(upmsApplication);
  }

  private void check(UpmsApplication upmsApplication) {
    this.checkNotNul(upmsApplication);
    assertService.isTrue(
        !CoreConstant.DELETE_FLAG_NOT.equals(upmsApplication.getDeleteFlag()),
        UpmsApplication.class.getName() + ".not-exist-error",
        "权限应用不存在");
    assertService.isFalse(
        UpmsApplicationStatusEnum.NORMAL.getCode().equals(upmsApplication.getStatus()),
        UpmsApplication.class.getName() + ".not-exist-error",
        "权限应用不存在");
  }

  private void checkNotNul(UpmsApplication upmsApplication) {
    assertService.isNull(upmsApplication, UpmsApplication.class.getName() + ".not-exist-error", "权限应用不存在");
  }
}
